﻿using AutoMapper;
using Hotel.Common;
using Hotel.Domain;
using Hotel.IRepository;
using Hotel.IServices;
using Hotel.IServices.RequestDTO;
using Hotel.IServices.ResponseDTO;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.Services
{
    public class AddUserServuces : IAddUserServuces
    {
        private readonly IBaseRepository<User> UserRepository;
        private readonly ILogger<AddUserServuces> logger;
        private readonly IMapper mapper;

        public AddUserServuces(IBaseRepository<User> userRepository, ILogger<AddUserServuces> logger, IMapper mapper)
        {
            UserRepository = userRepository;
            this.logger = logger;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult> AddUser(AddUserAsyncDto param)
        {
            try
            {
                var apiresult = new ApiResult("新增数据失败");

                var entity = mapper.Map<User>(param);

                var result = await UserRepository.InsertDataAsync(entity);

                if (result) apiresult.Success();

                return apiresult;
            }
            catch (Exception ex)
            {
                logger.LogError("InsertDatasErros:" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 分页 显示 用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult<PagingResult<IList<GetUserResDto>>>> ShowUser(GetUserPageDto dto)
        {
            var result = new ApiResult<PagingResult<IList<GetUserResDto>>>("查询失败");

            //获取用户数据
            var query = await UserRepository.GetAllAsync();

            //总条数
            var TotalCount = query.Count();

            //总页数
            var PageCount = (int)Math.Ceiling(TotalCount * 1.0 / dto.PaheSize);

            //分页数据
            query = query.OrderBy(p => p.UserId).Skip((dto.PageIndex - 1) * dto.PaheSize).Take(dto.PaheSize).ToList();

            //将数据映射到dto
            var data = mapper.Map<IList<GetUserResDto>>(query);

            //返回标准的分页数据
            var page = new PagingResult<IList<GetUserResDto>>();
            page.totalPage = PageCount;
            page.totalCount = TotalCount;
            page.data = data;
            result.Success(page);

            return result;
        }



        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public async Task<ApiResult> DelUser(long uid)
        {
           var result=   new ApiResult("删除失败");

            //根据id查询用户
           var data=await UserRepository.GetAsync(p => p.UserId == uid);
            if (data!=null&&data.UserId >0)
            {
                //删除标准位为1：删除  0:正常
                data.IsDel = 1;

           
             var res=   await UserRepository.UpdateDataAsync(data);
                if(res)
                {
                    result.Success();
                }
            }

            return result;
        }


        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="Uid"></param>
        /// <returns></returns>
      public async Task<ApiResult> UpSqlUser(long Uid)
        {
            var result=new ApiResult<User>("查询失败");
           var info= await UserRepository.GetAsync(p => p.UserId == Uid);
            if (info!=null&&info.UserId > 0) {
                  result.Success(info);
            }
            return result;
        }


        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult> UpUser(GetUpUserResDto dto)
        {
            var result = new ApiResult("修改失败");
            var info=await UserRepository.GetAsync(p => p.UserId == dto.UserId);
            if (info != null && info.UserId > 0)
            {
                //替换原来的数据
             var data=   mapper.Map(dto, info);
              var query= await UserRepository.UpdateDataAsync(data);
                if (query)
                {
                    result.Success();

                }
            }
            return result;
        }
            

    }
}
